use loco_rs::prelude::*;
use sea_orm::DeleteResult;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

pub use super::_entities::attribute_values::{self, ActiveModel, Entity, Model};

#[derive(Debug, Deserialize, Serialize)]
pub struct CreateParams {
    pub attribute_id: String,
}

#[derive(Debug, Validate, Deserialize)]
pub struct Validator {
    #[validate(length(min = 1, message = "Attribute ID must not be empty."))]
    pub attribute_id: String,
}

impl Validatable for ActiveModel {
    fn validator(&self) -> Box<dyn Validate> {
        Box::new(Validator {
            attribute_id: self.attribute_id.as_ref().to_owned(),
        })
    }
}

#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
    async fn before_save<C>(self, _db: &C, insert: bool) -> Result<Self, DbErr>
    where
        C: ConnectionTrait,
    {
        self.validate()?;
        if insert {
            let mut this = self;
            this.id = Set(Uuid::new_v4().to_string());
            Ok(this)
        } else {
            Ok(self)
        }
    }
}

impl Model {
    /// finds an attribute value by the provided id
    ///
    /// # Errors
    ///
    /// When could not find attribute value or DB query error
    pub async fn find_by_id(db: &DatabaseConnection, id: &str) -> ModelResult<Self> {
        let attribute_value = attribute_values::Entity::find()
            .filter(
                model::query::condition()
                    .eq(attribute_values::Column::Id, id)
                    .build(),
            )
            .one(db)
            .await?;
        attribute_value.ok_or_else(|| ModelError::EntityNotFound)
    }

    /// finds attribute values by the provided attribute id
    ///
    /// # Errors
    ///
    /// When could not find attribute values or DB query error
    pub async fn find_by_attribute_id(
        db: &DatabaseConnection,
        attribute_id: &str,
    ) -> ModelResult<Vec<Self>> {
        let attribute_values = attribute_values::Entity::find()
            .filter(
                model::query::condition()
                    .eq(attribute_values::Column::AttributeId, attribute_id)
                    .build(),
            )
            .all(db)
            .await?;
        Ok(attribute_values)
    }

    /// Asynchronously creates an attribute value and saves it to the database.
    ///
    /// # Errors
    ///
    /// When could not save the attribute value into the DB
    pub async fn create(db: &DatabaseConnection, params: &CreateParams) -> ModelResult<Self> {
        let attribute_value = ActiveModel {
            attribute_id: ActiveValue::set(params.attribute_id.to_string()),
            ..Default::default()
        }
        .insert(db)
        .await?;

        Ok(attribute_value)
    }

    /// Deletes the attribute value
    ///
    /// # Errors
    ///
    /// When could not delete the attribute value from the DB
    pub async fn delete(self, db: &DatabaseConnection) -> ModelResult<DeleteResult> {
        let model: ActiveModel = self.into();
        Ok(model.delete(db).await?)
    }
}

impl ActiveModel {
    /// Updates the attribute value's attribute id
    ///
    /// # Errors
    ///
    /// when has DB query error
    pub async fn update_attribute_id(
        mut self,
        db: &DatabaseConnection,
        attribute_id: String,
    ) -> ModelResult<Model> {
        self.attribute_id = ActiveValue::set(attribute_id);
        Ok(self.update(db).await?)
    }
}
